"""

Template module for Rooms

Copy this module up one level and name it as you like, then
use it as a template to create your own Objects.

To make the default commands (such as @dig) default to creating rooms
of your new type, change settings.BASE_ROOM_TYPECLASS to point to
your new class, e.g.

settings.BASE_ROOM_TYPECLASS = "game.gamesrc.objects.myroom.MyRoom"

Note that objects already created in the database will not notice
this change, you have to convert them manually e.g. with the
@typeclass command.

"""

from ev import Room, utils
from game.gamesrc.utils import myutils
from game.gamesrc.objects.object import MyObject

class MyRoom(MyObject, Room):
    """
    Rooms are like any Object, except their location is None
    (which is default). They also use basetype_setup() to
    add locks so they cannot be puppeted or picked up.
    (to change that, use at_object_creation instead)

    See examples/object.py for a list of
    properties and methods available on all Objects.
    """

    def return_appearance(self, pobject):
        """
        This is a convenient hook for a 'look'
        command to call.
        """

        string = super(MyRoom, self).return_appearance(pobject)

        if not pobject:
            return
        # get and identify all objects
        visible = (con for con in self.contents if con != pobject and con.access(pobject, "view"))
        exits, users, things = [], [], []
        cols = [[],[]]

        for content in visible:
            key = content.key
            if content.destination: #exits
                if hasattr(content, "is_closed") and content.is_closed:
                    cols[0].append(key)
                    cols[1].append(content.db.door_name.capitalize())
                    continue           
                if hasattr(content.destination, "is_dark") and content.destination.is_dark:
                    cols[0].append(key)
                    cols[1].append("E' molto buio qui...")
                    continue      
                dest=""
                if 'WILD' in content.destination.flags:
                    dest = content.destination.layers.get('name').get(content.db.wild_dest)
                else:
                    dest = content.destination.name
                cols[0].append(key)
                cols[1].append(dest.capitalize())
            elif content.has_player: #players
                users.append(key)
            else: #things
                things.append(content.r_desc)


        things = list(myutils.tile_objects(things))


        if cols[0]:
            ftable = utils.format_table(cols)
            string += "\n{wUscite:{n"
            for row in ftable:
                string += "\n " + "%s- %s" % (row[0], row[1])
        if users or things:
            string += "\n{wStai vedendo:\n{n"
            if users:
                string += "{c" + "\n".join(users) + "{n\n"
            if things:
                string += "\n".join(things)
        return string

    def at_object_receive(self, moved_obj, source_location):
        """
        Called after an object has been moved into this object.

        moved_obj - the object moved into this one
        source_location - where moved_object came from.
        """
        #turns off the lights in the source room when the guy with the torch leaves
        if source_location.scripts and 'DARK' in source_location.flags:
            source_location.scripts.validate()
